home *** CD-ROM | disk | FTP | other *** search
- ------------------------------------------------------------------------------
- -- File: apackdemo
- -- Description: aPLib binding demo (Q&D!)
- -- Date/version: 9.III.1999
- -- Author: Gautier.deMontmollin@Maths.UniNe.CH
- ------------------------------------------------------------------------------
-
- with APLib;
- with Ada.Command_Line; use Ada.Command_Line;
- with Ada.Text_IO; use Ada.Text_IO;
- with Ada.Direct_IO;
-
- procedure APack_Demo is
- package IIO is new Integer_IO(integer); use IIO;
-
- type byte is mod 2 ** 8; for byte'size use 8; -- could be any basic data
-
- type t_data_array is array(integer range <>) of byte;
- type p_data_array is access t_data_array;
-
- -- NB: File management is simpler with Ada95 Stream_IO - it's to test...
-
- package DBIO is new Ada.Direct_IO(byte); use DBIO;
- subtype file_of_byte is DBIO.File_type;
-
- procedure Read_file(n: String; d: out p_data_array) is
- f : file_of_byte; b: byte;
- begin
- d:= null;
- Open(f, in_file, n);
- d:= New t_data_array(1..integer(size(f)));
- for i in d'range loop Read(f,b); d(i):= b; end loop;
- Close(f);
- exception
- when DBIO.Name_Error => Put_Line("File " & n & " not found !");
- end;
-
- procedure Write_file(n: String; d: t_data_array) is
- f : file_of_byte;
- begin
- Create(f, out_file, n);
- for i in d'range loop Write(f,d(i)); end loop;
- Close(f);
- end;
-
- procedure Test_pack_unpack(name: string; id: natural) is
- ext1: constant string:= integer'image(id+1000);
- ext: constant string:= ext1(ext1'last-2..ext1'last); -- 000 001 002 etc.
- name_p: constant string:= "packed." & ext;
- name_pu: constant string:= "pack_unp." & ext;
-
- frog, frog2, frog3: p_data_array;
- pl, ul: integer; -- packed / unpacked sizes in _bytes_
-
- pack_occur: natural:= 0;
-
- procedure Packometer(u,p: integer; continue: out boolean) is
- li: constant:= 50;
- pli: constant integer:= (p*li)/ul;
- uli: constant integer:= (u*li)/ul;
- fancy_1: constant string:=" .oO";
- fancy_2: constant string:="|/-\";
- fancy: string renames fancy_2; -- choose one...
- begin
- Put(" [");
- for i in 0..pli-1 loop put('='); end loop;
- put(fancy(fancy'first+pack_occur mod fancy'length));
- pack_occur:= pack_occur + 1;
- for i in pli+1..uli loop put('.'); end loop;
- for i in uli+1..li loop put(' '); end loop;
- Put("] " & integer'image((100*p)/u)); Put("% " & ASCII.CR);
- continue:= true;
- end;
-
- procedure Pack(u: t_data_array; p: out t_data_array; pl: out integer) is
- subtype tu is t_data_array(u'range);
- subtype tp is t_data_array(p'range);
- package aplb is new APLib(tp, tu, Packometer);
-
- begin
- aplb.Pack(u,p,pl);
- end;
-
- procedure Depack(p: t_data_array; u: out t_data_array) is
- subtype tu is t_data_array(u'range);
- subtype tp is t_data_array(p'range);
- package aplb is new APLib(tp, tu, Packometer);
-
- begin
- aplb.Depack(p,u);
- end;
-
- bytes_per_element: constant integer:= byte'size/8;
-
- begin
- New_Line;
-
- Read_file(name, frog);
-
- if frog /= null then
- ul:= frog.all'size / 8; -- frog.all is the array; ul= size in bytes
-
- frog2:= New t_data_array(1..(((ul * 9) / 8) + 16) / bytes_per_element);
- -- recommended length
-
- Put_Line("File name: " & name);
- New_Line;
- Pack(frog.all, frog2.all, pl);
-
- New_Line;
- Put("Unpacked size: "); Put(ul); New_Line;
- Put("Packed size: "); Put(pl); New_Line;
- Put("Compression ratio: "); Put((100*pl)/ul,0); Put_Line("%");
-
- Put_Line("Packed file name: " & name_p);
- Write_file(name_p, frog2(1..pl));
-
- frog3:= New t_data_array(frog'range); -- MUST be the same data!
- Depack(frog2(1..pl), frog3.all);
-
- Put_Line("Packed & unpacked file name: " & name_pu);
- Write_file(name_pu, frog3.all);
- end if;
-
- end Test_pack_unpack;
-
- begin
- Put_Line("APack_Demo");
- Put_Line("Command: apacdemo file1 file2 file3 ...");
- Put_Line("When no file, frog.bmp is loaded");
-
- if Argument_count=0 then
- Test_pack_unpack("frog.bmp",0);
- else
- for i in 1..Argument_count loop
- Test_pack_unpack(Argument(i),i);
- end loop;
- end if;
- end APack_Demo;
-